package com.cet.test.mt01;

import java.util.*;

/**
 * @program: cet-practice
 * @description:
 * @author: 陈恩涛
 * @create: 2023-04-04 20:41
 **/
public class Client {

    public static void main(String[] args) {
        Org a = new Org(1L, "A", null);
        Org b = new Org(2L, "B", 1L);
        Org c = new Org(3L, "C", 5L);
        Org e = new Org(4L, "E", null);
        Org f = new Org(5L, "F", 2L);
        Org g = new Org(6L, "G", 4L);

        List<Org> orgs = Arrays.asList(a, b, c, e, f, g);

        transferOrgBo(orgs).forEach(System.out::println);
    }

    /**
     * a -> a
     * b -> a/b
     * c -> a/b/f/c
     * @param orgs
     * @return
     */
    static List<OrgBo> transferOrgBo(List<Org> orgs) {
        if (orgs == null || orgs.size() == 0) {
            return new ArrayList<>();
        }

        Map<Long, OrgBo> pathMap = new HashMap<>();

        int size = 0;

        while (true) {
            for (Org org : orgs) {
                // 根节点加入map
                if (org.getParentId() == null) {
                    pathMap.put(org.getId(), new OrgBo(org.getId(),org.getName(),org.getParentId(), org.getName()));
                }

                // 下级节点加入map
                if (pathMap.containsKey(org.getParentId())) {
                    pathMap.put(org.getId(),new OrgBo(org.getId(),org.getName(),org.getParentId(),pathMap.get(org.getParentId()).getPath() + "/" + org.getName()));
                }
            }
            if (pathMap.size() == size) {
                 break;
            }
            size = pathMap.size();
        }
        List<OrgBo> orgBos = new ArrayList<>();
        pathMap.forEach((key, value) -> {
            orgBos.add(value);
        });
        return orgBos;
    }
}
